//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vPelleteuse.h"
#include "uSKiloutou.h"
#include "dm_Kiloutou.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

bool __fastcall GerePelleteuse( TOperationSaisie nOpe)
{
    bool bRetour = false;
    TvSPelleteuse *fPelleteuse = NULL;
    try {
        fPelleteuse = new TvSPelleteuse( Application, nOpe );
        if (  (nOpe == OPE_Ajout)
            ||(nOpe == OPE_Modif)) {
            bRetour = (fPelleteuse->ShowModal() == mrOk);
        } else if (nOpe == OPE_Suppr) {
            if (MessageDlg("Etes-vous sūr(e) ?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes) {
                fPelleteuse->Supprimer();
                bRetour = true;
            }
        } else {
            assert( false );
        }

    } catch(...) {
        ShowMessage("Erreur !!!");
    }
    if (fPelleteuse) { delete fPelleteuse; fPelleteuse = NULL; }
    return bRetour;
}
//---------------------------------------------------------------------------
__fastcall TvSPelleteuse::TvSPelleteuse(TComponent* Owner, TOperationSaisie nOpe)
    : TForm(Owner)
{

    m_nOpe = nOpe;
    if (nOpe == OPE_Modif) {
        // Affiche la pelleteuse ą modifier
        eNom->Text                   = asMateriel(NOM);
        eDescription->Text           = asMateriel(DESCRIPTION);
        ePoidsTotal->Text            = fPelleteuse(POIDSTOTAL);
        ePrix->Text                  = asMateriel(PRIX);
    }
    else {
        eNom->Clear();
        eDescription->Clear();
        ePoidsTotal->Clear();
        ePrix->Clear();
    }

}
//---------------------------------------------------------------------------
void __fastcall TvSPelleteuse::bbOKClick(TObject *Sender)
{
    switch(m_nOpe)
    {
        case OPE_Ajout:
            Enregistrer();
            break;
        case OPE_Modif:
            Modifier();
            break;
    }
}

//---------------------------------------------------------------------------

void __fastcall TvSPelleteuse::Enregistrer()
{
    dmKiloutou->tMateriel->Insert();
    asMateriel(NOM)             = eNom->Text;
    asMateriel(DESCRIPTION)     = eDescription->Text;
    asMateriel(PRIX)            = ePrix->Text.ToDouble();
    nMateriel(TYPE_MATERIEL)    = TYPE_Pelleteuse;
    dmKiloutou->tMateriel->Post();
    dmKiloutou->trKiloutou->CommitRetaining();

    dmKiloutou->tMateriel->Refresh();
    dmKiloutou->tMateriel->Last();


    dmKiloutou->tPelleteuse->Insert();
    fPelleteuse(POIDSTOTAL)         = ePoidsTotal->Text.ToDouble();
    nPelleteuse(CODE_MATERIEL)      = nMateriel(CODE_MATERIEL);
    dmKiloutou->tPelleteuse->Post();
    dmKiloutou->trKiloutou->CommitRetaining();
}

//---------------------------------------------------------------------------

void __fastcall TvSPelleteuse::Modifier()
{
    dmKiloutou->tMateriel->Edit();
    asMateriel(NOM)             = eNom->Text;
    asMateriel(DESCRIPTION)     = eDescription->Text;
    asMateriel(PRIX)            = ePrix->Text.ToDouble();
    dmKiloutou->tMateriel->Post();
    dmKiloutou->trKiloutou->CommitRetaining();

    dmKiloutou->tPelleteuse->Insert();
    fPelleteuse(POIDSTOTAL)     = ePoidsTotal->Text.ToDouble();
    dmKiloutou->tPelleteuse->Post();
    dmKiloutou->trKiloutou->CommitRetaining();
}

//---------------------------------------------------------------------------

void __fastcall TvSPelleteuse::Supprimer()
{
    dmKiloutou->tMateriel->Delete();
    dmKiloutou->trKiloutou->CommitRetaining();

    dmKiloutou->tPelleteuse->Delete();
    dmKiloutou->trKiloutou->CommitRetaining();
}

//---------------------------------------------------------------------------
